package com.weddingee.action.space;

import java.util.List;
import com.weddingee.action.base.BaseAction;
import com.weddingee.dao.user.UserDao;
import com.weddingee.model.User;
import com.weddingee.model.Other;
import com.weddingee.model.PhotoGroup;
import com.weddingee.util.DaoFactory;
import com.weddingee.util.cond.UserCondition;
import com.weddingee.dao.home.OtherDao;
import com.weddingee.dao.home.PhotoGroupDao;



public class SpaceAction extends BaseAction{
	private UserDao userDao = (UserDao)DaoFactory.create(6);
	private OtherDao otherDao = (OtherDao)DaoFactory.create(3);
	private PhotoGroupDao photoGroupDao = (PhotoGroupDao)DaoFactory.create(5);
	private Long uid;
	private Long otherId;
	private User user;
	private Other other;
	private String type;
	private int profileIndex =1 ;
	private int messageIndex=1;
	private int direction;
	private List<User> userList;
	private List<Other> otherList;
	private List<PhotoGroup> photoGroupList;
	
	public int getDirection() {
		return direction;
	}

	public void setDirection(int direction) {
		this.direction = direction;
	}

	public int getMessageIndex() {
		return messageIndex;
	}

	public void setMessageIndex(int messageIndex) {
		this.messageIndex = messageIndex;
	}

	public String mySpace(){
		if( null == uid){
			uid = getLoginUser().getUserId();
			return ERROR;
		}
		
		user = userDao.findByID(uid);
		type = user.getType();
		if(type.equals("婚礼摄影")){
		photoGroupList = photoGroupDao.listPhotoGroup(uid);
		}
		if( null == user ){
			log.error("用户不存在。");
			return ERROR;
		}
		userDao.addVisitTimes(user.getUserId());
		return SUCCESS;
	}
	
	public String showOther(){
		if( null == otherId){
			log.info("参数错误。");
			return ERROR;
		}
		
		other = otherDao.find(otherId);
		type = other.getType();
		if( null == other ){
			log.error("数据不存在。");
			return ERROR;
		}
		//userDao.addVisitTimes(user.getUserId());
		return SUCCESS;
	}
    
	//找下一个或者上一个
    //找别的人
    public String findAnother(){
    	if( null == uid){
			uid = getLoginUser().getUserId();
			return ERROR;
		}
    	User auser =new User();
		auser = userDao.findByID(uid);
		String type = auser.getType();
		UserCondition cond = new UserCondition();
		cond.setPage(1);
		cond.setCount(1000);
		cond.setType(type);
		List<User> userList = userDao.listUser(cond);
		int size = userList.size();
		int position =0;
		for(int i=0;i<size;i++){
			User temp = userList.get(i);
			if(temp.getUserId().equals(uid)){
				position=i;
				break;
			}
		}
		
		
		if(direction==1){
			position++;
		}
		if(direction==0){
			position--;
		}
	
		if(-1<position&&position<size){
			user = userList.get(position);
		}
		if(position<0||position==size){
			user=auser;
		}
		uid= user.getUserId();
    	return SUCCESS;
    }
    //随机找一个人看看
    //
    public String findRandom(){
    	if( null == uid){
			uid = getLoginUser().getUserId();
		}
    	User auser =new User();
		auser = userDao.findByID(uid);
		String type = auser.getType();
		UserCondition cond = new UserCondition();
		cond.setPage(1);
		cond.setCount(1000);
		cond.setType(type);
		userList = userDao.listUser(cond);
		int size = userList.size();
		int position=(int)(Math.random()*size);
		user = userList.get(position);
		uid= user.getUserId();
    	return SUCCESS;
    }
	
	public Long getUid() {
		return uid;
	}

	public void setUid(Long uid) {
		this.uid = uid;
	}

	public User getUser() {
		return user;
	}

	public void setProfileIndex(int profileIndex) {
		this.profileIndex = profileIndex;
	}

	public int getProfileIndex() {
		return profileIndex;
	}



	public Other getOther() {
		return other;
	}



	public Long getOtherId() {
		return otherId;
	}



	public void setOtherId(Long otherId) {
		this.otherId = otherId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public List<PhotoGroup> getPhotoGroupList() {
		return photoGroupList;
	}

	public void setPhotoGroupList(List<PhotoGroup> photoGroupList) {
		this.photoGroupList = photoGroupList;
	}
	
}
